<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.7.1.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.core.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.widget.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.position.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.autocomplete.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.7.1.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-timepicker-addon.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-timepicker-zh-CN.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.datepicker-zh-TW.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/Validform_v5.3.2_ncr_min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/artdialog/jquery.artDialog.js"></script>
<style>
    #txtHint {
        position: absolute;
        top: 30px;
        left: 0;
        display: none;
        background-color: #EDFAF4;
        min-width: 281px;
        border: 1px solid #aaa;
        border-radius: 5px;
        height: 150px;
        overflow-x: hidden;
    }

    #txtHint li {
        list-style: none;
        color: #333;
        border: 1px solid #EDFAF4;
        padding: 0px 8px;
    }

    #txtHint li:hover {
        color: #c37100;
        background-color: #fdf8d5;
        border: 1px solid #fac900;
        border-radius: 25px;
    }

    #detail span:hover {
        color: #c37100;
    }

    #group {
        position: relative;
    }

    .table1 thead tr th, .table1 tbody tr td {
        text-align: center;
    }
</style>
<body id="pageContent" class="well">
<div class="form-horizontal">
    <div class="control-group">
        <label class="control-label" for="input01">单据号码:</label>

        <div class="controls">
            <input type="text" class="input-xlarge" id="purchaseorder_id" value="<?php if(isset($purchaseorder_id)){
                echo $purchaseorder_id;
            } ?>" readonly="true" autocomplete="off"/>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="supplier_name">供应商:</label>

        <div class="controls" id="group">
            <input type="text" class="input-mini" name="supplier_id" id="supplier_id" value="" autocomplete="off"
                   readonly="readonly"/>
            <input type="text" class="input-xlarge" name="supplier_name" id="supplier_name" value=""
                   placeholder="请输入供应商"
                   autocomplete="off"/>
            <button onclick="window.location.reload()">重选</button>
        </div>
    </div>
    <div class="control-group" id="select_supplier_contact_div">
        <label class="control-label" for="supplier_contact">联系人:</label>

        <div class="controls">
            <select class="input-xlarge" name="supplier_contact" id="supplier_contact" style="width: 284px">
                <option value="0">选择联系人</option>
            </select>
        </div>
    </div>
    <div class="control-group" id="supplier_payway_div">
        <label class="control-label" for="input01">付款方式:</label>

        <div class="controls">
            <select class="input-xlarge" name="paytype" id="select_payway" style="width: 284px">
                <option value="0">选择付款方式</option>
            </select>
        </div>
    </div>
    <div class="control-group" id="select_paybankinfo_div">
        <label class="control-label" for="select_paybankinfo">付款帐号:</label>

        <div class="controls">
            <select class="input-xlarge" name="select_paybankinfo" id="select_paybankinfo" style="width: 284px">
                <option value="0">选择付款帐号</option>
            </select>
        </div>
    </div>

    <!-- 仓库选择----------yuqing 2015-08-11--------------------------------->

    <div class="control-group" id="select_store_div">
        <label class="control-label" for="select_storeinfo">仓库:</label>

        <div class="controls">
            <select class="input-xlarge" name="select_store" id="select_store" style="width: 284px">
                <option value="-1">--请选择--</option>
                <option value="0">--国内仓--</option>
                <?php if(!empty($stors)){
                    foreach($stors as $stor_id=>$stor){?>
                    <option value="<?php echo $stor_id;?>">--<?php echo $stor;?>--</option>
                <?php }}?>
            </select>
        </div>
    </div>

    <!-- ---------------------------------------------------------------->
    <div class="control-group">
        <label class="control-label" for="input01">运费:</label>

        <div class="controls">
            <input type="text" class="input-xlarge" onkeyup="clearNoNum(this)" name="transportation_expense"
                   autocomplete="off"
                   id="transportation_expense" value="0"/>
        </div>
    </div>
    <div class="control-group rateTypeControl hidden">
        <label class="control-label">汇率类型:</label>

        <div class="controls">
            <select id="rateType" name="rateType" class="input-xlarge" style="width: 284px"">
                <option value="0">请选择汇率类型</option>
                <?php foreach($types as $tId=>$t){?>
                    <option value="<?php echo $tId;?>"><?php echo $t;?></option>
                <?php }?>
            </select>
        </div>
    </div>
    <div class="control-group rateControl hidden">
        <label class="control-label">汇率值:</label>

        <div class="controls">
            <input type="text" class="input-xlarge" onkeyup="clearNoNum(this)" name="coinRate"
                   autocomplete="off"
                   id="coinRate" value="0"/>
        </div>
    </div>
    <div class="control-group">

        <!-- Textarea -->
        <label class="control-label">备注:</label>

        <div class="controls">
            <textarea type="" class="" style="margin: 0px; width: 272px; height: 90px;resize:none;" name="remark"
                      autocomplete="off"
                      id="remark"> </textarea>
        </div>
    </div>
    <div class="control-group" style="display:none">
        <label class="control-label" for="input01"></label>

        <div class="controls">
            <input type="text" class="input-xlarge" name="supplier" id="supplier" value="" autocomplete="off"/>
        </div>
    </div>
</div>
<table class="table table1 table-hover table-striped table-condensed">
    <thead>
    <tr style="  background-color:#4EA392; color:#fff;">
        <th>
            SKU
        </th>
        <th>
            中文名称
        </th>
        <th>
            单位
        </th>
        <th>
            储位
        </th>
        <th>
            计税方式
        </th>
        <th>
            单价<span style="color: red">￥</span>
        </th>
        <th>
            数量
        </th>
        <th>
            税率
        </th>
        <th>
            金额(￥)
        </th>
        <th>
            交货日期
        </th>
        <th>
            系统成本价
        </th>
        <th>
            状态
        </th>
        <th>
            <button class="btn btn-info" id="add_tr_btn" onclick="add()">新增</button>
        </th>

    </tr>
    </thead>
    <tbody id="row">

    <tr id="1">
        <td>
            <input style="width:70px;" class="input-mini sku_input" type="text" value="" autocomplete="off"
                   onblur="check_sku(this)"
                   onpaste="check_sku(this)"
                   onfocus="do_autocomplete(this)"/>
        </td>
        <td>

        </td>
        <td>

        </td>
        <td>

        </td>

        <td>
            <select class="input-xlarge tax_way" name="tax_way" id="tax_way" style="width: 75px" autocomplete="off"
                    onchange="count_money(this)">
                <option value="0">不计税</option>
                <option value="-1">内含税</option>
                <option value="1">外加税</option>
            </select>
        </td>
        <td>
            <input class="input-mini" type="text" value='' style="width:40px" onkeyup="clearNoNum(this)"
                   onblur="count_money_and_price(this)"
                   onafterpaste="clearNoNum(this)" autocomplete="off"/>
        </td>
        <td>
            <input class="input-mini" type="text" value='' style="width:40px" onkeyup="clearNoNum(this)"
                   autocomplete="off" onblur="count_money_in_count(this)"
                   onafterpaste="clearNoNum(this)"/>
        </td>
        <td>
            <input class="input-mini tax_rate" onkeyup="clearNoNum(this)" type="text" value='0'
                   onblur="count_money(this)"
                   autocomplete="off"/>%
        </td>
        <td class="all_m">
            <input class="input-mini" type="text" value='' disabled="disabled" autocomplete="off"/>
        </td>
        <td>
            <input class="input-mini datepicker" type="text" value='' style="width:80px" autocomplete="off"/>
        </td>
        <td>

        </td>
        <td>

        </td>
        <td>
            <button class="btn btn-info" onclick="delete_remove(this)">删除</button>
        </td>
    </tr>
    </tbody>

</table>
<div>
    <span style="display: none" id="tr_num_add">1</span>
    <span style="display: none" id="tr_num">1</span>
    <button id="tijiao" class="btn btn-info" style="margin-left:10px;">一键创建</button>
</div>
</body>
<script type="text/javascript">

/**
 * 显示供应商详细信息
 * 联系人
 * 付款方式
 * 付款帐号
 */
function show_supplier_info(supplier_info) {
    if (supplier_info.supplier_contanct) {
        /**
         * 修改替换联系人选择框
         */
        $("#supplier_contact").empty();
        $("#supplier_contact").append(supplier_info.supplier_contanct);
    }

    if (supplier_info.supplier_payway) {
        /**
         * 修改替换付款方式选择框
         */
        $("#select_payway").empty();
        $("#select_payway").append(supplier_info.supplier_payway);
    }


    if (supplier_info.supplier_paybank) {
        /**
         * 修改替换付款帐号选择框
         */
        $("#select_paybankinfo").empty();
        $("#select_paybankinfo").append(supplier_info.supplier_paybank);
    }

    if(supplier_info.coinid) {
        if(supplier_info.coinid != 1) {
            $(".rateTypeControl").removeClass("hidden");
        }
    }
}


/**
 * 显示供应商的SKU信息，sku信息
 * 指定的行号
 */
function show_supplier_sku_info(id, supplier_sku_info) {
    if (supplier_sku_info) {
        if (supplier_sku_info.sku) {
            $("#" + id).children().eq(0).find("input").val(supplier_sku_info.sku);
        }
        if (supplier_sku_info.sku_name) {
            $("#" + id).children().eq(1).text(supplier_sku_info.sku_name);
        }
        if (supplier_sku_info.nuit) {
            $("#" + id).children().eq(2).text(supplier_sku_info.nuit);
        }
        if (supplier_sku_info.place) {
            $("#" + id).children().eq(3).text(supplier_sku_info.place);
        }
        if (supplier_sku_info.price) {
            $("#" + id).children().eq(5).find("input").val(supplier_sku_info.price);
        }
        if (supplier_sku_info.delivery) {
            $("#" + id).children().eq(9).find("input").val(supplier_sku_info.delivery);
        }
        if (supplier_sku_info.sku_price) {
            $("#" + id).children().eq(10).text(supplier_sku_info.sku_price);
        }
        if (supplier_sku_info.status) {
            $("#" + id).children().eq(11).text(supplier_sku_info.status);
        }
    }
}


/**
 * 获取创建采购单的供应商信息
 */
function get_purchase_info_supplier() {
    var supplier_id = $('#supplier_id').val();
    if (supplier_id == 0) {
        alert('请选择供应商');
        return false;
    }

    var supplier_contact = $("#supplier_contact").val();
    if (supplier_contact == 0) {
        alert('请选择供应商联系人');
        return false;
    }
    var select_payway = $("#select_payway").val();
    if (select_payway == 0) {
        alert('请选择供应商付款方式');
        return false;
    }

    var select_paybankinfo = $("#select_paybankinfo").val();
    if (select_paybankinfo == 0) {
        alert('请选择供应商付款帐号');
        return false;
    }
    /*
    * yuqing
    * 检测仓库是否选定
    */

    var select_store = $("#select_store").val();
    if(select_store==-1){
        alert('请选择仓库');
        return false;
    }
    /*
     * yuqing
     *
     */
    var coinRate = 0;
    var rateType = 0;
    if($(".rateTypeControl").hasClass("hidden")) {
    }else{
        var rateType = $("#rateType").val();
        if(rateType == 0) {
            alert('请选择汇率类型');
            return false;
        }

        if(rateType == 40) {
            if($("#coinRate").val() != 0 && $("#coinRate").val()){
                coinRate = $("#coinRate").val();
            }else{
                alert('请填写汇率值');
                return false;
            }
        }
    }

    var remark = $("#remark").val();
    var transportation_expense = $("#transportation_expense").val();
    /**
     *  供应商信息
     *  供应商id
     *  付款方式id
     *  付款帐号id
     *  运费
     *  备注
     */
    var supplier_info = {"supplier_id": supplier_id,
        "contact_id": supplier_contact,
        "pay_way": select_payway,
        "payaccountinfo_id": select_paybankinfo,
        "transportation_expense": transportation_expense,
        "remark":remark,
        "rateType":rateType,
        "coinRate": coinRate,
        "createtype": 1,
        'store':select_store
    };

    return supplier_info;
}

/**
 *  获取某一行的数据
 *
 */
function get_purchase_sku_info_row(id) {
    if ($("#" + id).children('td').find('input').attr('readonly')) {
        var sku_info = {
            "sku": $("#" + id).children('td').eq(0).find("input").val(),
            "sku_name": $("#" + id).children('td').eq(1).text(),
            "storage_position": $("#" + id).children('td').eq(3).text(),
            "sku_standard": "",
            "quantity": $("#" + id).children('td').eq(6).find('input').val(),
            "single_price": $("#" + id).children('td').eq(5).find('input').val(),
            "tax_rate": $("#" + id).children('td').eq(7).find('input').val(),
            "remark": "",
            "arrive_date": $("#" + id).children('td').eq(9).find('input').val(),
            "tax_way": $("#" + id).children('td').eq(4).find("select").val(),
            "sku_unit": $("#" + id).children('td').eq(2).text(),
            "prime_cost": $("#" + id).children('td').eq(10).text(),
            "supplier": $('#supplier_id').val()
        }

        return sku_info;
    }
    else {
        return false;
    }
}

/**
 * 获取采购单的SKU信息
 */
function get_purchase_sku_info() {
    var sku_info_arr = [];
    $("#row").children('tr').each(function () {
        var sku_info = get_purchase_sku_info_row($(this).attr('id'));
        if (sku_info) {
            sku_info_arr.push(sku_info);
        }
    });
    if (sku_info_arr) {
        return sku_info_arr;
    }
    else {
        alert('请选择SKU');
        return false;
    }
}

/**
 * 选中sku之后再补全sku信息
 */
function get_supplier_sku_info(supplier_id, sku, obj) {
    var id = $(obj).parent().parent().attr('id');
    if (supplier_id
        && sku) {
        $.ajax(
            {
                type: 'post',
                url: '<?php echo url::base(true)?>purchase/getskuinfo',
                dataType: 'json',
                data: {"supplier_id": supplier_id, "sku": sku},
                async: false,
                success: function (re_data) {
                    if (re_data) {
                        show_supplier_sku_info(id, re_data);
                    }
                },
                error: function () {
                }
            }
        );
    }
}

/**
 * 根据供应商ID查询供应商的信息
 * @param supplier_name
 */
function get_supplier_info(supplier_id) {
    $.ajax(
        {
            type: 'post',
            url: '<?php echo url::base(true)?>purchase/supplierpayway',
            dataType: 'json',
            data: {"supplier_id": supplier_id},
            async: false,
            success: function (re_data) {
                if (re_data) {
                    show_supplier_info(re_data);
                }
            },
            error: function () {
            }
        }
    );
}


/**
 * 查询供应商自动提示补全
 */
$("#supplier_name").autocomplete({
    autoFocus: true,
    minLength: 0,
    autoFill: true,
    source: '<?php echo URL::base(true)?>purchase/get_supplier',
    select: function (event, ui) {
        /**
         * 从建议列表中选中之后触发的事件
         */
        $("#supplier_name").val(ui.item.name);
        $("#supplier_id").val(ui.item.id);
        $("#supplier_name").attr("readonly", "readonly");
        get_supplier_info(ui.item.id);
        return false;
    }
});

/**
 * 获取SKU数据
 */
function check_sku(obj) {
    var supplier_id = $("#supplier_id").val();
    var sku = $(obj).val();
    if (supplier_id != ''
        && sku != ''
        && !$(obj).attr('readonly')) {
        $.ajax(
            {
                type: 'post',
                url: '<?php echo url::base(true)?>purchase/getskuinfo',
                dataType: 'json',
                data: {"supplier_id": supplier_id, "sku": sku},
                async: false,
                success: function (re_data) {
                    if (re_data && re_data.sku) {
                        $(obj).val(re_data.sku);
                        $(obj).attr("readonly", "readonly");
                        get_supplier_sku_info(supplier_id, re_data.sku, obj);
                        //yuqing 去掉注释
                        checkSkuDou();
                    }
                    else {
//                        $(obj).focus();
                        alert('该供应商没有找到这个SKU信息');
                        return false;
                    }
                },
                error: function () {
                }
            }
        );
    }
}

/**
 * 获取供应商数据
 */
function check_supplier(obj) {

}
function checkSkuDou() {
    var skuArr = [];

    $("#row tr").each(function () {
        var temp = $(this).children("td").eq(0).find("input[readonly='readonly']").val();
        if (!temp) {
            return false;
        }
        skuArr.push(temp);
    });

    skuArr = skuArr.sort();

    for (var i = 0; i < skuArr.length; i++) {
        if (skuArr[i] === skuArr[i + 1]) {
            alert("SKU有重复项请审核");
            return false;
        }
    }
    return true;
}

/**
 * 自动提示供应商的sku信息
 */
function do_autocomplete(obj) {
    var supplier_id = $("#supplier_id").val();
    if (supplier_id != ''
        && !$(obj).attr('readonly')
        && !$(obj).data("autocomplete")) {
        $(obj).autocomplete({
            autoFocus: true,
            minLength: 0,
            autoFill: true,
            source: '<?php echo URL::base(true)?>purchase/get_supplier_sku?supplier_id=' + supplier_id,
            select: function (event, ui) {
                /**
                 * 从建议列表中选中之后触发的事件
                 */
                $(obj).val(ui.item.sku);
                $(obj).attr("readonly", "readonly");
                get_supplier_sku_info(supplier_id, ui.item.sku, this);
                checkSkuDou();
                return false;
            }
        });
    }
}

/**
 * 设置时间控件
 */
$.datepicker.setDefaults({ dateFormat: 'yy-mm-dd' });

$(document).on('focus', ".datepicker", function (e) {
    $(this).datepicker({
        minDate: new Date()
    });
});

function checkDouSku() {
    var skuArr = [];

    $("#row tr").each(function () {
        var temp = $(this).children("td").eq(0).find("input[readonly='readonly']").val();
        if (!temp) {
            alert("SKU有空项");
            return false;
        }
        skuArr.push(temp);
    });

    skuArr = skuArr.sort();

    for (var i = 0; i < skuArr.length; i++) {
        if (skuArr[i] === skuArr[i + 1]) {
            alert("SKU有重复项请审核");
            return false;
        }
    }

    return true;
}

$("#tijiao").click(function () {
    var supplier_info = get_purchase_info_supplier();
    if (supplier_info == false)return false;
    if (!checkDouSku()) return false;
    //if(!rateOrNot())return false;
    var sku_info = get_purchase_sku_info();
    if (supplier_info && sku_info) {
        $.ajax(
            {
                type: 'post',
                url: '<?php echo url::base(true)?>purchase/createpurchasenew',
                dataType: 'json',
                data: {"purchaseorders": supplier_info, "purchaseorder_details": sku_info},
                async: false,
                success: function (re_data) {
                    if (re_data.code == 2) {
                        var tips = '创建成功，采购单号' + re_data.id;
                        alert(tips);
                        window.location.reload();
                    }
                },
                error: function () {
                }
            }
        );
    }
});

$("#select_paybankinfo").change(function(){
    if($(".rateTypeControl").hasClass("hidden")){
        $(".rateTypeControl").removeClass("hidden");
    }else{
        $(".rateTypeControl").addClass("hidden");
    }
});
/**
 * 显示添加的列
 */
function show_add_row(data) {
    if (data.num) {
        $("#tr_num_add").text(data.num);
        var num = parseInt($("#tr_num").text()) + 1;
        $("#tr_num").text(num);
    }
    if (data.view) {
        $("#row").append(data.view);
    }
}

/*
 *
 * 新增一列
 *
 * */
function add() {
    var num = $("#tr_num_add").text();
    $.ajax(
        {
            type: 'post',
            url: '<?php echo url::base(true)?>purchase/supplier_addrow',
            dataType: 'json',
            data: {"num": num},
            async: false,
            success: function (re_data) {
                if (re_data) {
                    show_add_row(re_data);
                }
            },
            error: function () {
            }
        }
    );
}


/**
 * 删除一列
 * @param obj
 */
function delete_remove(obj) {
    var id = parseInt($(obj).parent('td').parent('tr').attr('id'));
    var num = parseInt(parseInt($("#tr_num").text()));
    if (id > 0 && --num > 0) {
        $("#" + id).remove();
        $("#tr_num").text(num);
    }
}

/**
 * 获取计算金额的信息
 */
function get_money_info(obj) {
    var id = $(obj).parent('td').parent('tr').attr('id');

    if (!$("#" + id).children('td').eq(0).find('input').attr('readonly')) {
        return false;
    }
    var money_info = {
        "quantity": $("#" + id).children('td').eq(6).find('input').val(),
        "single_price": $("#" + id).children('td').eq(5).find('input').val(),
        "tax_rate": $("#" + id).children('td').eq(7).find('input').val(),
        "tax_way": $("#" + id).children('td').eq(4).find("select").val()
    }

    return money_info;
}

function show_money(obj, money) {
    if (money) {
        var id = $(obj).parent('td').parent('tr').attr('id');
        $("#" + id).children('td').eq(5).find('input').val(money.single_price);
        $("#" + id).children('td').eq(6).find('input').val(money.quantity);
        $("#" + id).children('td').eq(7).find('input').val(money.tax_rate);
        $("#" + id).children('td').eq(8).find('input').eq(0).val(money.money);
    }
}

function check_single_price(obj) {
    var id = $(obj).parent('td').parent('tr').attr('id');

    if (!$("#" + id).children('td').eq(0).find('input').attr('readonly')) {
        return false;
    }
    var sku = $("#" + id).children('td').eq(0).find("input").val();
    var single_price = $("#" + id).children('td').eq(5).find('input').val();
    if (single_price) {
        $.ajax(
            {
                type: 'post',
                url: '<?php echo url::base(true)?>purchase/check_single_price',
                dataType: 'json',
                data: {"single_price": single_price, 'sku': sku},
                async: false,
                success: function (re_data) {
                    if (re_data.code == 2) {
                        alert('注意：单价高于基础资料系统单价');
                    }
                },
                error: function () {
                }
            }
        );
    }
}

function check_sku_status(obj) {
    var id = $(obj).parent('td').parent('tr').attr('id');

    if (!$("#" + id).children('td').eq(0).find('input').attr('readonly')) {
        return false;
    }
    var sku = $("#" + id).children('td').eq(0).find("input").val();
    $.ajax(
        {
            type: 'post',
            url: '<?php echo url::base(true)?>purchase/check_sku_status',
            dataType: 'json',
            data: {'sku': sku},
            async: false,
            success: function (re_data) {
                if (re_data.code == 2) {
                    alert(re_data.mes);
                }
            },
            error: function () {
                alert('系统错误');
            }
        }
    );
}

/**
 * 计算价格与基础资料价格进行比较，如果大于，则不允许下单
 */
function count_money_and_price(obj) {
    check_single_price(obj);
    var money_info = get_money_info(obj);
    if (money_info) {
        $.ajax(
            {
                type: 'post',
                url: '<?php echo url::base(true)?>purchase/count_money',
                dataType: 'json',
                data: {"money_info": money_info},
                async: false,
                success: function (re_data) {
                    if (re_data) {
                        show_money(obj, re_data);
                    }
                },
                error: function () {
                }
            }
        );
    }
}
//输入数量后提示
function count_money_in_count(obj) {
    var td_single_price = $(obj).parent().prev().find("input");
    check_sku_status(td_single_price);
    check_single_price(td_single_price);
    var money_info = get_money_info(obj);
    if (money_info) {
        $.ajax(
            {
                type: 'post',
                url: '<?php echo url::base(true)?>purchase/count_money',
                dataType: 'json',
                data: {"money_info": money_info},
                async: false,
                success: function (re_data) {
                    if (re_data) {
                        show_money(obj, re_data);
                    }
                },
                error: function () {
                }
            }
        );
        //num_blur();
    }
}

/**
 * 重新计算金额
 */
function count_money(obj) {
    check_single_price();
    var money_info = get_money_info(obj);
    if (money_info) {
        $.ajax(
            {
                type: 'post',
                url: '<?php echo url::base(true)?>purchase/count_money',
                dataType: 'json',
                data: {"money_info": money_info},
                async: false,
                success: function (re_data) {
                    if (re_data) {
                        show_money(obj, re_data);
                    }
                },
                error: function () {
                }
            }
        );
    }
}

function clearNoNum(obj) {
    obj.value = obj.value.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符

    obj.value = obj.value.replace(/^\./g, "");  //验证第一个字符是数字而不是.

    obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.

    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
}

function change_txt(money, coin) {
    return '财务需折算为' + coin +'付款，汇率以付款当天汇率为准。';
}

//修改运费后备注自动加上
function text_money_change(obj) {
    if($(".all_m").length) {
        var coin = $("#coin").val();
        if(parseInt(coin) > 1) {
            var f = 0;
            $(".all_m").each(function(){
                if(parseFloat($(this).children("input").eq(0).val())) {
                    f += parseFloat($(this).children("input").eq(0).val());
                }
            });
            if(f) {
                var coin_name = $("#coin").children("option[value='" + coin +"']").text();
                f += parseFloat($(obj).val());
                $("#remark").val(change_txt(f, coin_name));
            }
        }
    }
}

function coin_change(obj) {
    //判断是否有有效的SKU行
    if($(obj).val()<=1) {
        var reg=/^.*?当天汇率.*?$/;
        if(reg.test($("#remark").val())) {
            if(window.confirm("确定修改为非外币吗，修改后备注将清空？")) {
                $("#remark").val("");
            }
        }
        return;
    }

    if($(".all_m").length) {
        var f = 0;
        $(".all_m").each(function(){
            if(parseFloat($(this).children("input").eq(0).val())) {
                f += parseFloat($(this).children("input").eq(0).val());
            }
        });
        if(f) {
            var coin = $(obj).val();
            var coin_name = $(obj).children("option[value='" + coin +"']").text();
            f += parseFloat($("#transportation_expense").val());
            $("#remark").val(change_txt(f, coin_name));
        }
    }
}

function num_blur() {
    //有数量的时候判断币种
    if($(".all_m").length) {
        var coin = $("#coin").val();
        if(parseInt(coin) > 1) {
            var f = 0;
            $(".all_m").each(function(){
                if(parseFloat($(this).children("input").eq(0).val())) {
                    f += parseFloat($(this).children("input").eq(0).val());
                }
            });
            if(f) {
                var coin_name = $("#coin").children("option[value='" + coin +"']").text();
                f += parseFloat($("#transportation_expense").val());
                $("#remark").val(change_txt(f, coin_name));
            }
        }
    }
}

function rateOrNot() {
    var rateType = $("#rateType").val();
    if(rateType == 0){
        alert("请选择汇率类型");
        return false;
    }else{
        if($("#rateType").val() == 40) {
            if($("#coinRate").val()){
            }else{
                alert("请填写汇率值");
                return false;
            }
        }
    }
    return true;
}

$("#rateType").change(function(){
    if($(this).val() == 40){
        $(".rateControl").removeClass('hidden');
    }else{
        $(".rateControl").addClass('hidden');
    }
});

</script>

